<?php
// 数据库配置
$host = 'localhost';
$dbname = 'itops_help1_center';
$username = 'itops_help1_center';
$password = 'itops_help1_center';

$categories = []; // 分类列表
$articles = []; // 文章列表
$error = '';

try {
    $pdo = new PDO(
        "mysql:host=$host;dbname=$dbname;charset=utf8mb4",
        $username,
        $password,
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );

    // 获取所有分类（用于首页分类展示）
    $stmt = $pdo->query("SELECT id, name FROM categories ORDER BY sort_order ASC");
    $categories = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // 获取首页文章列表（原有逻辑）
    $stmt = $pdo->query("SELECT a.*, c.name as category_name 
                        FROM article a 
                        LEFT JOIN categories c ON a.category_id = c.id 
                        WHERE a.status = 1 
                        ORDER BY a.create_time DESC");
    $articles = $stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
    $error = "加载失败：" . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>知识库 - 首页</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="css/common.css">
</head>
<body>
    <div class="front-container">
        <header>
            <h1>IT知识库</h1>
            <p>整理各类操作指南和说明文档</p>
        </header>

        <!-- 分类展示区（修复链接路径和参数） -->
        <div class="categories-section" style="margin: 2rem 0;">
            <h2 style="margin-bottom: 1rem;">知识分类</h2>
            <div class="categories-list" style="display: flex; gap: 1rem; flex-wrap: wrap;">
                <?php foreach ($categories as $category): ?>
                    <!-- 修复点：链接指向正确的category.php，并携带分类ID（id=<?php echo $category['id']; ?>） -->
                    <a href="category.php?id=<?php echo $category['id']; ?>" 
                       style="display: inline-block; padding: 0.75rem 1.5rem; background: #F1F5F9; 
                              border-radius: 0.5rem; color: #1E293B; text-decoration: none;">
                        <?php echo htmlspecialchars($category['name']); ?>
                    </a>
                <?php endforeach; ?>
                <?php if (empty($categories)): ?>
                    <p style="color: #64748B;">暂无分类</p>
                <?php endif; ?>
            </div>
        </div>

        <!-- 文章列表区（原有逻辑） -->
        <div class="article-list">
            <h2 style="margin-bottom: 1rem;">最新文章</h2>
            <?php foreach ($articles as $article): ?>
                <div class="article-card">
                    <h3 class="article-title">
                        <a href="article_detail.php?id=<?php echo $article['id']; ?>">
                            <?php echo htmlspecialchars($article['title']); ?>
                        </a>
                    </h3>
                    <div class="article-meta">
                        <span><i class="fas fa-folder"></i> <?php echo htmlspecialchars($article['category_name'] ?? '未分类'); ?></span>
                        <span><i class="fas fa-calendar"></i> <?php echo $article['create_time']; ?></span>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</body>
</html>